﻿//using Leadshine;
using System;
using System.Runtime.InteropServices;

namespace MotionIoLib
{
   


    public class IoCtrl_LTDMC3400 : IoCtrl
    {
        private ushort _ConnecNo = 0;

        public IoCtrl_LTDMC3400(int nIndex, ulong nCardNo)
            : base(nIndex, nCardNo)
        {
        }

        public override void DeInit()
        {
        }

        public override bool Init()
        {
            m_bOpen = false;
            _ConnecNo = (ushort)m_nCardNo;
            ushort n_COpenNum = (ushort)m_nIndex;
            if (n_COpenNum>0)
            {
                short res = LTDMC.dmc_board_init();
                res = LTDMC.nmc_set_connect_state((ushort)_ConnecNo, n_COpenNum, 0, 0);//断开控制器
                res = LTDMC.nmc_set_connect_state((ushort)_ConnecNo, n_COpenNum, 1, 0);//连接控制器
                if (res != 0)
                {

                    return false;
                }
            }
            m_bOpen = true;
            return true;
        }

        public override bool InStopDisenable(int nIndex)
        {
            throw new NotImplementedException();
        }

        public override bool InStopEnable(int nIndex)
        {
            throw new NotImplementedException();
        }


        public override bool ReadIOIn(ref int nData)
        {
            nData = (int)LTDMC.dmc_read_inport(_ConnecNo, 0);
            return true;
        }

        public override bool ReadIoInBit(int nIndex)
        {
            int nAxisIndex = (nIndex & 0xff00) >> 8;
            int nBitIndex = nIndex & 0x00ff;
            short res = -1;
            //输入口
            //res = LTDMC.dmc_read_inbit(_ConnecNo, (ushort)nIndex);

            if (nAxisIndex == 0)
            {
                //输入口
                res = LTDMC.dmc_read_inbit(_ConnecNo, (ushort)nIndex);
            }
            else 
            {
                ushort ioValue = 0;
                //输入口
                res = LTDMC.nmc_read_inbit(_ConnecNo, (ushort)nAxisIndex, (ushort)nBitIndex, ref ioValue);
                res = (short)ioValue;

            }

            return res == 0;
        }

        public override bool ReadIOOut(ref int nData)
        {
            throw new NotImplementedException();
        }

        public override bool ReadIoOutBit(int nIndex)
        {
            int nAxisIndex = (nIndex & 0xff00) >> 8;
            int nBitIndex = nIndex & 0x00ff;
            short res = -1;
            if (nAxisIndex == 0)
            {
                //输入口
                res = LTDMC.dmc_read_outbit(_ConnecNo, (ushort)nIndex);
            }
            else 
            {
                ushort ioValue = 0;
                //输入口
                res = LTDMC.nmc_read_outbit(_ConnecNo, (ushort)nAxisIndex, (ushort)nBitIndex, ref ioValue);
                res = (short)ioValue;

            }

            return res == 0;
        }

        public override bool WriteIo(int nData)
        {
            throw new NotImplementedException();
        }

        public override bool WriteIoBit(int nIndex, bool bBit)
        {



            int nAxisIndex = (nIndex & 0xff00) >> 8;
            int nBitIndex = nIndex & 0x00ff;


            if (nAxisIndex == 0)
            {
                short ret = LTDMC.dmc_write_outbit(_ConnecNo, (ushort)nBitIndex, bBit ? (ushort)0 : (ushort)1);
            }
            else
            {

                short ret = LTDMC.nmc_write_outbit(_ConnecNo, (ushort)nAxisIndex, (ushort)nBitIndex, bBit ? (ushort)0 : (ushort)1);
            }





            return true;
        }
    }






}